package com.hkts.naturalgas.base.controller;


import com.hkts.naturalgas.base.entity.*;
import com.hkts.naturalgas.utils.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hkts.naturalgas.base.service.IOzoneService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * 臭氧
 *
 * @author yck
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/instrumentdata/ozone")
public class OzoneController {
    private final Logger logger = LoggerFactory.getLogger(OzoneController.class);

    @Autowired
    public IOzoneService iOzoneService;

    /**
     * 分页查询数据
     *
     * @param page  分页信息
     * @param ozone 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getOzonePageList")
    public Result<Object> getHontUserList(Page<Ozone> page, Ozone ozone) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iOzoneService.selectPage(page, ozone)));
    }

    /**
     * 保存和修改公用的
     *
     * @param ozone 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/ozoneSave")
    public Result<Object> ozoneSave(Ozone ozone) {
        iOzoneService.saveOrUpdate(ozone);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/ozoneGetById")
    public Result<Object> ozoneGetById(String id) {
        Ozone byId = iOzoneService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/ozoneDelete")
    public Result<Object> hontUserDelete(String id) {
        iOzoneService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/ozoneBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iOzoneService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }
    @GetMapping("/getPageByTime")
    public Result<Object> getPageByTime(Page<Ozone> page, String startDate, String endDate){
        return ResultUtil.data(CustomPageUtils.getCustomPage(iOzoneService.selectPageByTime(page,startDate,endDate)));
    }
    /** 功能描述:
    *
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-10 11:04
    * @Description //TODO
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/divisionData")
    @Transactional
    public Result<java.lang.Object> divisionData() throws Exception {
        Ozone ozone = new Ozone();
        List<Ozone> ozones = iOzoneService.selectList(ozone);
        iOzoneService.deleteAll();
        for (int i = 0; i < ozones.size(); i++) {
            if (i != ozones.size() - 1) {
                LocalDateTime time = ozones.get(i).getTime();
                System.out.println(time);
                Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
                List<LocalDateTime> allHourse = DataHandlerUtils.getAllHourse(date);
                List<Ozone> list = new ArrayList<>();
                //1#臭氧流量
                List<Float> dataSsy1 = DataHandlerUtils.getHandlerData(ozones.get(i).getBsjhCy1(), ozones.get(i+1).getBsjhCy1());
                List<Float> dataSsyc1 = DataHandlerUtils.getHandlerData(ozones.get(i).getBsjhCy1c(), ozones.get(i+1).getBsjhCy1c());
                for(int j=0;j<allHourse.size();j++){
                    if(j==0){
                        Ozone ozone1 = ozones.get(i);
                        ozone1.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()) );
                        list.add(ozone1);
                    }else{
                        Ozone calcinationPurifyWetElectricity =new Ozone();
                        calcinationPurifyWetElectricity.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                        calcinationPurifyWetElectricity.setTime(allHourse.get(j));
                        calcinationPurifyWetElectricity.setBsjhCy1(dataSsy1.get(j));
                        calcinationPurifyWetElectricity.setBsjhCy1c(dataSsyc1.get(j));
                        list.add(calcinationPurifyWetElectricity);
                    }
                }
               iOzoneService.saveBatch(list);
            }else{
                ozones.get(i).setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
                iOzoneService.save(ozones.get(i));
            }
        }
        return ResultUtil.success("处理成功");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/importPaperData")
    public Result<Object> importPaperData(String url) throws Exception {
        File excelFile = new File(url);
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row = sheet.getRow(2);

        List<Ozone> list=new ArrayList<Ozone>(800);
        for (int i=1 ; i <sheet.getLastRowNum(); i++) {


            HSSFRow row1 = sheet.getRow(i);
            if(row1.getCell(0)==null){
                break;
            }
            Date dateCellValue = row1.getCell(0).getDateCellValue();
            System.out.println(dateCellValue);
            row1.getCell(0).setCellType(CellType.STRING);
            String time=row1.getCell(0).getStringCellValue();
            Ozone Ozone=new Ozone();
            Ozone.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            Ozone.setTime(dateCellValue.toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDateTime());
            row1.getCell(1).setCellType(CellType.STRING);
            Ozone.setBsjhCy1(Float.parseFloat(row1.getCell(1).getStringCellValue()));
            row1.getCell(2).setCellType(CellType.STRING);
            Ozone.setBsjhCy1c(Float.parseFloat(row1.getCell(2).getStringCellValue()));
            list.add(Ozone);
        }
        iOzoneService.saveBatch(list);
        return ResultUtil.success("处理成功！");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/addData")
    @Transactional
    public Result<java.lang.Object> addData( String beginDate, String endDate) throws ParseException {
        List<LocalDateTime> allMinites = DataHandlerUtils.getAllMinites(beginDate, endDate);
        List<Ozone> electricity =new ArrayList<Ozone>();
        for(int i=0;i<allMinites.size();i++){
            Ozone Ozone=new Ozone();
            Ozone.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            Ozone.setTime(allMinites.get(i));
            Ozone.setBsjhCy1c(0f);
            Ozone.setBsjhCy1(0f);
            electricity.add(Ozone);
        }
      //  iOzoneService.saveBatch(electricity);
        return ResultUtil.success("处理成功！");
    }
}
